package com.ltz.service.well;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ltz.dao.well.WellDao;
import com.ltz.dao.well.WellGroupDao;
import com.ltz.model.MonthlyProductData;
import com.ltz.model.ObservationRecord;
import com.ltz.model.Page;
import com.ltz.model.Pager;
import com.ltz.model.User;
import com.ltz.model.WellBasicData;
import com.ltz.model.WellGroup;

@Service("wellService")
public class WellService {
	@Resource
	private WellDao wellDao;
	//web
	public Page loadList(Page page,String queryStr){
		String sql="select * from well_basic_data where name like ? order by date_input desc";
		return wellDao.loadList(page,sql,new Object[]{"%"+queryStr+"%"});
	}
	
	public boolean doAddWell(User user,WellBasicData w){
		w.setId(null);
		w.setDateInput(new Date());
		w.setOwner(user.getUsername());
		wellDao.add(w);
		return true;
	}
	public boolean doUpdateWell(User user,WellBasicData w){
		WellBasicData rec=(WellBasicData) wellDao.get(WellBasicData.class, w.getId());
		w.setDateInput(rec.getDateInput());
		w.setOwner(rec.getOwner());
		wellDao.update(w);
		return true;
	}
	public boolean doDeleteWell(int wellGroupId){
		wellDao.delete(WellBasicData.class, wellGroupId);
		return true;
	}
	//flex
	/**
	 * 查询所有井和井组
	 */
	public List findAll(){
		List types=wellDao.getAll(WellGroup.class);
		String sql="select * from well_basic_data where group_id=?";
		if(null!=types){
			List<WellBasicData> list;
			WellGroup wg;
			for(int i=0; i<types.size(); i++){
				wg=(WellGroup)types.get(i);
				list=wellDao.nativeQueryForList(sql, new Object[]{wg.getWellGroupId()},WellBasicData.class);
				wg.setWellList(list);
			}
			list=wellDao.nativeQueryForList(sql, new Object[]{0},WellBasicData.class);
			wg=new WellGroup();
			wg.setWellGroupName("闲置井");
			wg.setWellList(list);
			types.add(wg);
		}
		return types;
	}
	/**
	 * 查询打井交班记录
	 * @param wellId
	 * @param pager
	 * @return
	 */
	public Map<String, Object> findObsReordByPage(int wellId,Pager pager) {
		Map<String, Object> map=new HashMap<String,Object>();
		Page page=new Page();
		page.setPageSize(pager.getPageSize());
		page.setPageNo(pager.getCurrPN());
		try{
			String sql="select * from observation_record where well_id=? order by date_input desc";
			page=wellDao.findByPage(page,sql,new Object[]{wellId},ObservationRecord.class);
			pager.setTotalNo(page.getCount());
			map.put("data", page.getList());
			map.put("pager", pager);
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}
	public WellBasicData saveWell(WellBasicData w){
		WellBasicData tmp=(WellBasicData) wellDao.update(w);
		return tmp;
	}
	public ObservationRecord addObsRecord(ObservationRecord obs){
		obs.setId(null);
		obs.setDateInput(new Date());
		obs=(ObservationRecord) wellDao.add(obs);
		return obs;
	}
	public ObservationRecord saveObsRecord(ObservationRecord obs){
		obs=(ObservationRecord) wellDao.update(obs);
		return obs;
	}
	public boolean deleteObsRecord(int recordId){
		wellDao.delete(ObservationRecord.class, recordId);
		return true;
	}
	public List findProductionRecord(Date date,int wellGroupId){
		return wellDao.findProductionRecord(date, wellGroupId);
	}
	public MonthlyProductData addPrdRecord(MonthlyProductData prd){
		prd.setId(null);
		prd.setDateInput(new Date());
		prd=(MonthlyProductData) wellDao.add(prd);
		return prd;
	}
	public MonthlyProductData savePrdRecord(MonthlyProductData prd){
		prd=(MonthlyProductData) wellDao.update(prd);
		return prd;
	}
	public void deletePrdRecord(int id){
		wellDao.delete(MonthlyProductData.class, id);
	}
}
